<?php

/* 
 * 权限分配
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
class ActionController extends AppController
{
    public function __construct() {
        parent::__construct();
        $this->modelObj = new AdminActionModel();
        $this->log_type = '权限管理';
        $this->cookie_key = 'action_cookie';
    }
    /**
     * 浏览
     */
    public function view(){
        $this->get_cookie();
        $list = AdminActionModel::load()->get_list_struct();
        $this->assign('struct', $list);
        $this->get_data();
        $this->display('action_list');
    }
    public function query(){
        $this->get_data();        
        $this->ret['status'] = 0;
        $this->ret['content'] = $this->fetch('action_list');
        exit(json_encode($this->ret));
    }
    /**
     * 获取数据
     */
    private function get_data(){
        $page = isset($_REQUEST['page']) ? $_REQUEST['page'] : 1;        
        $where = '';
        $filter['action_id'] = isset($_POST['action_id']) ? $_POST['action_id'] : -1;
        $filter['action_code'] = isset($_POST['action_code']) ? $_POST['action_code'] : '';
        $filter['action_url'] = isset($_POST['action_url']) ? $_POST['action_url'] : '';
        $filter['action_chinese'] = isset($_POST['action_chinese']) ? $_POST['action_chinese'] : '';
        if($filter['action_id'] >= 0) {
            $where .= ' and action_id = ' . $filter['action_id'] . ' or parent_id = ' . $filter['action_id'];
        }
        if(!empty($filter['action_code'])) {
            $where .= ' and instr(action_code, ' . "'{$filter['action_code']}')";
        }
        if(!empty($filter['action_url'])) {
            $where .= ' and instr(action_code, ' . "'{$filter['action_url']}')";
        }
        if(!empty($filter['action_chinese'])) {
            $where .= ' and instr(action_chinese, ' . "'{$filter['action_chinese']}')";
        }
        $this->set_cookie($filter);
        $sort_by = isset($_REQUEST['sort_by']) ? $_REQUEST['sort_by'] : $this->modelObj->keyFields;
        $sort_order = isset($_REQUEST['sort_order']) ? $_REQUEST['sort_order'] : 'DESC';
        $pageObj = new pageModel('admin_action', $where, $this->page_number, 10, $sort_by, $sort_order);        
        $data = $pageObj->pageData($page);           
        $list = $data['pageData'];
        foreach ($list as $key => $row) {
            $data['pageData'][$key]['id'] = $key + 1;
            $data['pageData'][$key]['is_menu'] = $row['is_menu'] == 1 ? 'glyphicon-ok' : 'glyphicon-remove';    
        }
        $this->assign('sort_' . $sort_by, $data['sort_img']);
        $this->assign('data', $data);
    }
    /**
     * 用户分配权限
     */
    public function allot(){
        $data = AdminActionModel::load()->get_list_struct();    
        if(isset($_GET['user_id'])) {
            $user_id = intval($_GET['user_id']);
            $user_row = AdminUserModel::load()->get_info($user_id);
            if($user_row) {
                if($user_row['is_admin'] == 1) {
                    $this->show_msg('管理员权限无法编辑');
                }
                $action_list_str = $user_row['action_list'];
                $user_name = $user_row['user_name'];
                $user_list_arr = explode(',', $action_list_str);
                foreach ($data as $key => $row) {
                    $childRow = $row['child'];
                    if($childRow) {
                        foreach ($childRow as $ckey => $crow) {
                            $child_action_code = $crow['action_code'];
                            foreach ($user_list_arr as $allot) {
                                if(trim($child_action_code) == trim($allot)) {
                                    $data[$key]['child'][$ckey]['is_allot'] = 1;
                                    break;
                                } else {
                                    $data[$key]['child'][$ckey]['is_allot'] = 0;                                
                                }
                            }
                        }                    
                    }
                }
                $this->assign('user_name', $user_name);
            }
            $this->assign('user_id', $user_id);
        }
        $this->assign('data', $data);
        $this->display('action_allot');
    }
    /**
     * 保存
     */
    public function new_allot(){        
        $user_id = isset($_POST['user_id']) ? intval($_POST['user_id']) : 0;
        $post_arr = $_POST['action_code'];
        if($user_id > 0) {
            $action_code_str = count($post_arr) > 0 ?  implode(',', $post_arr) : '';            
            $where = array(
                'user_id' => $user_id,
            );
            $data = array(
                'action_list' => $action_code_str,
            );            
            $bool = AdminUserModel::load()->update($data, $where);
            if($bool) {
                $this->show_msg('分配成功', 'c=user&m=view');
            }
        }
        $this->show_msg('分配失败');
    }
    
    
    /**
     * 显示
     */
    public function create(){
        $this->create_token();        
        $list = AdminActionModel::load()->get_list_struct();
        $this->get_cookie();
        $parent_id = isset($_GET['id']) ? $_GET['id'] : 0;
        $this->assign('parent_id', $parent_id);
        $this->assign('struct', $list);
        $this->display('action_create');
    }    
    /**
     * 创建
     */
    public function new_create() {
        $this->check_token();
        $parent_id = $_POST['action_id'];
        $data = array(
            'parent_id'      => $parent_id,
            'action_chinese' => $_POST['action_chinese'],
            'action_code'    => $_POST['action_code'],
            'action_url'     => $_POST['action_url'],
            'class_id'       => $_POST['class_id'],
            'method_id'      => $_POST['method_id'],
            'rel_class_id'   => $_POST['rel_class_id'],
            'rel_method_id'  => $_POST['rel_method_id'],
            'rel_action_code'=> $_POST['rel_action_code'],
            'is_menu'        => empty($_POST['is_menu']) ? 0 : 1,
        );        
        $this->set_cookie($data);
        if($parent_id > 0 && empty($_POST['action_chinese'])) {
            $this->show_msg('请填写名称');
        }
        if($parent_id > 0 && empty($_POST['class_id'])) {
            $this->show_msg('填写类名');
        }         
        if($parent_id > 0 && empty($_POST['method_id'])) {
            $this->show_msg('填写方法名');
        }
        //判断权限是否重复
        if(AdminActionModel::load()->checkCode($data['action_code'])) {
            $this->show_msg('权限代码:'.$data['action_code'].',已添加');
        }
        unset($data['class_id'], $data['method_id'], $data['rel_class_id'], $data['rel_method_id']);
        $bool = AdminActionModel::load()->create_action($data);
        if($bool) {
            $this->admin_log('添加权限:' . $data['action_chinese']);
            $this->show_msg('添加成功', 'c=action&m=view', false);
        } else {
            $this->show_msg('添加失败', 'c=action&m=create');
        }
    }
    public function edit() {
        $this->create_token();
        $this->get_cookie();
        $action_id = isset($_GET['id']) ? intval($_GET['id']) : 0;
        if($action_id > 0) {
            $info = AdminActionModel::load()->get_info($action_id);
            $list = AdminActionModel::load()->get_list_struct();
            $code_arr = explode(':', $info['action_code']);
            $this->assign('class_id', $code_arr[0]);
            $this->assign('method_id', $code_arr[1]);
            if($info['rel_action_code']) {
                $rel_code_arr = explode(':', $info['rel_action_code']);
                $this->assign('rel_class_id', $rel_code_arr[0]);
                $this->assign('rel_method_id', $rel_code_arr[1]);
            }            
            $this->assign('struct', $list);
            $this->assign('edit', $info);
            $this->display('action_edit');
        } else {
            $this->show_msg('操作失误');
        }
    }

    /**
     * 更新
     */
    public function new_edit(){
        $this->check_token();        
        $action_id = $_POST['action_id'];
        $parent_id = $_POST['parent_id'];
        $data = array(
            'parent_id'      => $parent_id,
            'action_chinese' => $_POST['action_chinese'],
            'action_code'    => $_POST['action_code'],
            'action_url'     => $_POST['action_url'],
            'class_id'       => $_POST['class_id'],
            'method_id'      => $_POST['method_id'],
            'rel_class_id'   => $_POST['rel_class_id'],
            'rel_method_id'  => $_POST['rel_method_id'],
            'rel_action_code'=> $_POST['rel_action_code'],
            'is_menu'        => empty($_POST['is_menu']) ? 0 : 1,
        );
        $this->set_cookie($data);
        if($parent_id > 0 && empty($_POST['action_chinese'])) {
            $this->show_msg('请填写名称');
        }
        if($parent_id > 0 && empty($_POST['class_id'])) {
            $this->show_msg('填写类名');
        }         
        if($parent_id > 0 && empty($_POST['method_id'])) {
            $this->show_msg('填写方法名');
        }
        unset($data['class_id'], $data['method_id'], $data['rel_class_id'], $data['rel_method_id']);
        $bool = AdminActionModel::load()->edit($action_id, $data);
        if($bool) {
            $this->admin_log('编辑权限:' . $data['action_chinese']);
            $this->show_msg('编辑成功', 'c=action&m=view', false);
        } else {
            $this->show_msg('添加失败', 'c=action&m=add_action');
        }
    }
}

